package com.study.a14Test;

public class MyRunnable implements Runnable {

    //套路
    //1.循环
    //2.同步代码块
    //3.判断共享数据是否到了末尾
    //4.判断共享数据是否到了末尾（没有到末尾执行核心逻辑）
    //
    int ticket = 0;

    @Override
    public void run() {
        while (true) {
            if (method()) {
                break;
            }
        }
    }

    private synchronized boolean method() {
        if (ticket == 1000) {
            return true;
        } else {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ticket++;
            System.out.println(Thread.currentThread().getName() + "在卖第" + ticket + "号票");
        }
        return false;
    }
}

